package com.cc.mall.controller;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cc.common.utils.PageUtils;
import com.cc.common.utils.ResponseData;
import com.cc.mall.entity.OfflineOrderEntity;
import com.cc.mall.feign.UserFeign;
import com.cc.mall.vo.OrderDetail;
import com.cc.user.entity.ScheduleEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cc.mall.entity.OnlineOrderEntity;
import com.cc.mall.service.OnlineOrderService;
import com.cc.common.utils.R;



/**
 * 
 *
 * @author leifengyang
 * @email leifengyang@gmail.com
 * @date 2020-12-09 20:57:52
 */
@RestController
@RequestMapping("mall/onlineorder")
@Api(tags="线上订单")
public class OnlineOrderController {
    @Autowired
    private OnlineOrderService onlineOrderService;

    @Autowired
    private UserFeign userFeign;


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		OnlineOrderEntity onlineOrder = onlineOrderService.getById(id);
        return R.ok().put("onlineOrder", onlineOrder);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody OnlineOrderEntity onlineOrder){
		onlineOrderService.save(onlineOrder);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody OnlineOrderEntity onlineOrder){
		onlineOrderService.updateById(onlineOrder);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		onlineOrderService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    // 后台线上订单接口
    @GetMapping("/getOnlineOrderList")
    public R getOnlineOrderList(@RequestParam Map<String,Object> params){
        Page<OnlineOrderEntity> page = onlineOrderService.getOnlineOrderList(params);
        return R.ok().put("page",page);
    }

    // 后台接口
    @GetMapping("/getOrderList")
    public R getOrderList(@RequestParam Map<String,Object> params){
        List<OfflineOrderEntity> offlineOrderEntities = onlineOrderService.getOrderList(params);

        int page = Integer.parseInt((String) params.get("page"));
        int limit = Integer.parseInt((String) params.get("limit"));
        int totalCount = offlineOrderEntities.size();
        if(offlineOrderEntities.size() == 0 || offlineOrderEntities == null){
            return R.ok().put("page",new PageUtils(null, totalCount, limit, page));
        }
        offlineOrderEntities = offlineOrderEntities.stream().sorted(Comparator.comparing(OfflineOrderEntity::getCreatetime).reversed()).collect(Collectors.toList());
        List<OfflineOrderEntity> list = new ArrayList<>();
        for(int i = (page-1)*limit;i<page*limit;i++){
            if(i<=offlineOrderEntities.size()-1)
                list.add(offlineOrderEntities.get(i));
        }
        PageUtils pageUtils = new PageUtils(list, totalCount, limit, page);
        return R.ok().put("page",pageUtils);
    }

    @GetMapping("/updateOnlineOrderStatus")
    public R updateOnlineOrderStatus(
            @RequestParam("id") Long id,
            @RequestParam("status") Integer status
    ) {
        OnlineOrderEntity onlineOrderEntity = new OnlineOrderEntity();
        onlineOrderEntity.setId(id);
        onlineOrderEntity.setOnlineOrderStatus(status);
        onlineOrderService.updateById(onlineOrderEntity);
        if(status == 2){
            ScheduleEntity scheduleEntity = new ScheduleEntity();
            scheduleEntity.setUid(onlineOrderEntity.getUid());
            scheduleEntity.setCreatetime(new Date());
            scheduleEntity.setScheduleStatus(0);
            scheduleEntity.setContent("您的订单【"+id+"】【已发货】");
            userFeign.save(scheduleEntity);
        }
        return R.ok();
    }
    @ApiOperation("改线上订单的价格")
    @GetMapping("/gaijia")
    public R gaijia(@ApiParam("线上订单id") @RequestParam("id") Long id,
                    @ApiParam("更改的价格") @RequestParam("price") String price){
        OnlineOrderEntity onlineOrderEntity = new OnlineOrderEntity();
        onlineOrderEntity.setId(id);
        onlineOrderEntity.setAmountsPayable(new BigDecimal(price));
        onlineOrderService.updateById(onlineOrderEntity);
        return R.ok();
    }

    @ApiOperation("获取线上订单详情")
    @GetMapping("/getOnlineOrderDetailByOrderId")
    public ResponseData<List<OrderDetail>> getOnlineOrderDetailByOrderId(@ApiParam("订单id") @RequestParam("orderId") Long orderId){
        List<OrderDetail> orderDetails = onlineOrderService.getOnlineOrderDetailByOrderId(orderId);
        return ResponseData.success(orderDetails);
    }

}
